<template>
  <div>
    <!-- 搜索功能 -->
    <el-row :gutter="20">
      <el-col :span="8">
        <el-input  placeholder="请输入内容"
        v-model="queryinfo.query" clearable
        @clear="getGoodsData">
          <el-button slot="append" icon="el-icon-search"
          @click="getGoodsData"></el-button>
        </el-input>
      </el-col>
      <!-- 添加商品 -->
      <el-col :span="4">
        <el-button type="primary" @click="openAddDialog"
      >添加商品<i class="el-icon-circle-plus-outline el-icon--right"></i
    ></el-button>
      </el-col>
    </el-row>

    <!-- 商品列表 -->
    <el-table :data="goodsDate" border stripe style="width: 100%">
      <el-table-column type="index" label="#" width="60px"> </el-table-column>
      <el-table-column prop="goods_name" label="商品名称"> </el-table-column>
      <el-table-column prop="goods_price" label="商品价格" width="80px">
      </el-table-column>
      <el-table-column prop="goods_weight" label="商品重量" width="80px">
      </el-table-column>
      <el-table-column label="创建时间" width="170px">
        <template slot-scope="scope">
          {{ scope.row.add_time | dateFormat }}
        </template>
      </el-table-column>
       <el-table-column label="操作" width="150px">
        <template slot-scope="scope">
          <!-- 修改按钮 -->
          <el-button
            type="warning"
            icon="el-icon-edit"
            circle
            @click="showGoodsEdit(scope.row)"
          ></el-button>
          <!-- 删除按钮 -->
          <el-button
            type="danger"
            icon="el-icon-delete"
            circle
            @click="removeGoodsEdit(scope.row.goods_id)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页按钮 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryinfo.pagenum"
      :page-sizes="[10, 20, 30, 50]"
      :page-size="queryinfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      background
    >
    </el-pagination>
      <!-- 修改用户信息弹出框 -->
    <el-dialog title="修改用户信息" :visible.sync="dialogGoodsEdit" width="60%">
      <el-form :model="goodsEditData" label-width="100px" class="demo-ruleForm">
        <el-form-item label="商品ID">
          <el-input v-model="goodsEditData.goods_id" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="商品名称">
          <el-input v-model="goodsEditData.goods_name"></el-input>
        </el-form-item>
        <el-form-item label="价格">
          <el-input v-model="goodsEditData.goods_price"></el-input>
        </el-form-item>
        <el-form-item label="数量">
          <el-input v-model="goodsEditData.goods_number"></el-input>
        </el-form-item>
        <el-form-item label="重量">
          <el-input v-model="goodsEditData.goods_weight"></el-input>
        </el-form-item>
        <el-form-item label="介绍">
          <el-input v-model="goodsEditData.goods_introduce"></el-input>
        </el-form-item>
        <el-form-item label="图片路径">
          <el-input v-model="goodsEditData.pics"></el-input>
        </el-form-item>
        <el-form-item label="商品参数">
          <el-input v-model="goodsEditData.attrs"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="editGoods">确 定</el-button>
        <el-button @click="editcancel">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  //import 引入组件
  components: {},
  data() {
    return {
      //所有商品列表
      goodsDate: [],
      //请求所有商品列表的参数
      queryinfo: {
        query: "",
        pagenum: 1,
        pagesize: 10,
      },
      //商品总数
      total: 0,
      dialogGoodsEdit:false,
      //编辑商品的数据
      goodsEditData:{}
    };
  },
  methods: {
    // 获取商品列表数据
    async getGoodsData() {
      const { data } = await this.$axios.get("/goods", {
        params: this.queryinfo,
      });
      // console.log(data);
      if (data.meta.status == 200) {
        this.goodsDate = data.data.goods;
        this.total = data.data.total;
      }
    },
    // 分页功能
    handleSizeChange(newsize) {
      this.queryinfo.pagesize = newsize;
      this.getGoodsData();
    },
    handleCurrentChange(newnum) {
      this.queryinfo.pagenum = newnum;
      this.getGoodsData();
    },
    //打开添加商品
    openAddDialog(){
      console.log('功能暂无');
    },
    //打开商品修改
    showGoodsEdit(info){},
    //删除商品
    async removeGoodsEdit(id){
       const confirmRes = await this.$confirm(
        "此操作将永久删除该商品, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);
      if (confirmRes !== "confirm") {
        return this.$message.info("已取消");
      }
      // 确认删除
      console.log(id);
      const {data}=await this.$axios.delete(`/goods/${id}`);
      console.log(data);
      if(data.meta.status!==200){
        this.$message.error(data.meta.msg);
      }else {
        this.$message({
          type: "success",
          message: data.meta.msg,
        });
        this.getGoodsData()
      }
    },
    //编辑商品
    showGoodsEdit(info){
      this.dialogGoodsEdit=true;
      this.goodsEditData=info
    },
    //编辑商品确认
    async editGoods(){
      this.dialogGoodsEdit=false
      const {data}=await this.$axios.put(`/goods/${this.goodsEditData.goods_id}`,this.goodsEditData)
      console.log(data);
    },
    //编辑商品取消
    editcancel(){
      this.dialogGoodsEdit=false
    },
  },
  mounted() {
    this.getGoodsData();
  },
};
</script>

<style scoped>
.search{
  width: 600px;
  display: flex;
  align-items: center;
}
</style>
